package com.prs.crm.action.oa;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.HomePageType;
import com.prs.crm.domain.oa.Memorabilia;
import com.prs.crm.domain.oa.MemorabiliaContent;
import com.prs.crm.service.oa.MemorabiliaContentService;
import com.prs.crm.service.oa.MemorabiliaService;

public class MemorabiliaAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private Memorabilia memorabilia;
	private MemorabiliaService memorabiliaService;
	private MemorabiliaContent memorabiliaContent;
	private MemorabiliaContentService memorabiliaContentService;
	private File upload;
	private String uploadFileName;
	private List<MemorabiliaContent> childs;
	private String title;
	
	@Action("listMemorabilia")
	public String listMemorabilia() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Memorabilia.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.memorabiliaService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editMemorabilia")
	public String editMemorabilia() {
		return SUCCESS;
	}
	@Action("editMemorabiliaContent")
	public String editMemorabiliaContent() {
		this.title=this.memorabilia.getTitle();
		return SUCCESS;
	}
	@Action("viewMemorabilia")
	public String viewMemorabilia() {
		this.childs=this.memorabilia.getContents();
		this.title=this.memorabilia.getTitle();
		this.memorabilia.setReadNum(memorabilia.getReadNum()+1);
		this.memorabiliaService.submit(memorabilia);
		return SUCCESS;
	}
	@Action(value = "saveMemorabilia", results = {
			@Result(name = "success", type = "redirect", location = "listMemorabilia"),
			@Result(name = "error", type = "chain", location = "editMemorabilia") })
	public String saveMemorabilia() {
		if(upload!=null){
			this.memorabilia.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if (memorabilia.getTitle()==null || "".equals(memorabilia.getTitle().trim()) ) {
			this.addActionError("请输入标题！");
			return ERROR; 
		}
		memorabilia.setSpkNum("0");
		memorabilia.setReadNum(0);
		Person person = AppContext.getAccountContext().currentUser();
		memorabilia.setLastName(person.getResume().getName());
		memorabilia.setLastTime(new Date());
		if(memorabilia.getId()==null){
			this.memorabiliaService.save(memorabilia);
		}else{
			this.memorabiliaService.submit(memorabilia);
		}
		return SUCCESS;
	}
	@Action(value = "saveMemorabiliaContent", results = {
			@Result(name = "success", type = "redirect", location = "viewMemorabilia?memorabilia=${memorabilia.getId()}"),
			@Result(name = "error", type = "chain", location = "editMemorabiliaContent?memorabilia=${memorabilia.getId()}") })
	public String saveMemorabiliaContent() {
		if(upload!=null){
			this.memorabiliaContent.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		int num=Integer.parseInt(memorabilia.getSpkNum())+1;
		memorabilia.setSpkNum(String.valueOf(num));
		Person person = AppContext.getAccountContext().currentUser();
		memorabilia.setLastName(person.getResume().getName());
		memorabilia.setLastTime(new Date());
		this.memorabiliaService.submit(memorabilia);
		//回复
		memorabiliaContent.setMemorabilia(memorabilia);
		this.memorabiliaContent.setTitle(title);
		this.memorabiliaContentService.save(memorabiliaContent);	
		return SUCCESS;
	}
	@Action(value = "removeMemorabilia", results = {
			@Result(name = "success", type = "redirect", location = "listMemorabilia"),
			@Result(name = "error", type = "chain", location = "viewMemorabilia?memorabilia=${memorabilia.getId()}") })
	public String removeMemorabilia(){
		if (memorabiliaContent!=null) {
			this.getMemorabiliaContentService().remove(memorabiliaContent);
		}
		this.getMemorabiliaService().remove(memorabilia);
		return SUCCESS;
	}
	@Action(value = "removeMeorabiliaContent", results = {
			@Result(name = "success", type = "redirect", location = "listMemorabilia"),
			@Result(name = "error", type = "chain", location = "viewMemorabilia?memorabilia=${memorabilia.getId()}") })
	public String removeMeorabiliaContent(){
		this.getMemorabiliaContentService().remove(memorabiliaContent);
		memorabiliaContent.setMemorabilia(memorabiliaContent.getMemorabilia());
		int num=Integer.parseInt(memorabiliaContent.getMemorabilia().getSpkNum())-1;
		memorabiliaContent.getMemorabilia().setSpkNum(String.valueOf(num));
		this.getMemorabiliaService().submit(memorabiliaContent.getMemorabilia());
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Memorabilia getMemorabilia() {
		return memorabilia;
	}
	public void setMemorabilia(Memorabilia memorabilia) {
		this.memorabilia = memorabilia;
	}
	public MemorabiliaService getMemorabiliaService() {
		return memorabiliaService;
	}
	public void setMemorabiliaService(MemorabiliaService memorabiliaService) {
		this.memorabiliaService = memorabiliaService;
	}
	public MemorabiliaContent getMemorabiliaContent() {
		return memorabiliaContent;
	}
	public void setMemorabiliaContent(MemorabiliaContent memorabiliaContent) {
		this.memorabiliaContent = memorabiliaContent;
	}
	public MemorabiliaContentService getMemorabiliaContentService() {
		return memorabiliaContentService;
	}
	public void setMemorabiliaContentService(
			MemorabiliaContentService memorabiliaContentService) {
		this.memorabiliaContentService = memorabiliaContentService;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public List<MemorabiliaContent> getChilds() {
		return childs;
	}
	public void setChilds(List<MemorabiliaContent> childs) {
		this.childs = childs;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

}
